package net.tngou.crawler.drug;



import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONObject;

import net.tngou.cache.CacheEngine;
import net.tngou.cache.TgCacheEngine;
import net.tngou.crawler.enums.StateEnum;
import net.tngou.crawler.pojo.Crawlerurl;
import net.tngou.crawler.util.Inflector;
import net.tngou.crawler.util.StringUtil;

public class BaseCrawler {
	protected  long onlinepharmacy;
	protected JSONObject json = new JSONObject(true);
	protected static final int SIZE=20;
	protected final String ID=getClass().getSimpleName();
	public void setId(long onlinepharmacy) {
		this.onlinepharmacy=onlinepharmacy;
	}
	public long getOnlinepharmacy() {
		return this.onlinepharmacy;
	}
	protected Long getId()
	{
		CacheEngine engine = new TgCacheEngine();//创建缓存    
		 Long id=(Long) engine.get(ID);
		 id=id==null?1:id+1;
		 engine.add(ID,  id);//存在  默认的储存库为 default
		 return id;	 
	}
	protected void clear()
	{
		CacheEngine engine = new TgCacheEngine();//创建缓存 
		engine.clear(ID);//清除缓存
	}
	
	/**
	 * 价格处理
	 * @param str
	 * @return
	 */
	protected float  getPrice(String str) {
	
		   if (StringUtil.isEmpty(str))return 0;
		   str=StringUtil.RemoveEmpty(str);
		   if (StringUtil.isEmpty(str))return 0;
		   str=str.replaceAll("￥", "").replaceAll("$", "").replaceAll("¥", "");
		   if (StringUtil.isEmpty(str))return 0;
		   str=StringUtil.split(str, " ")[0];
		   if (StringUtil.isEmpty(str))return 0;
		   str=StringUtil.split(str, "|")[0];
		   if(!NumberUtils.isNumber(str)) return 0;
		   return Float.parseFloat(str);
	 }
	
	public List<URL> urls() {
		Crawlerurl bean= new Crawlerurl();
		@SuppressWarnings("unchecked")
		List<Crawlerurl> ls = (List<Crawlerurl>) bean.list("state="+StateEnum.等待上传.ordinal()+" AND web="+getOnlinepharmacy(), 1, 100);
		List<URL> list = new ArrayList<>();
		ls.forEach(e->{			
			try {
				URL u = new URL(e.getUrl());
				list.add(u);
//				e.setState(StateEnum.上传完成.ordinal());
//				e.update();
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			}
			
		});		
		return list;
	}
	
	
	protected JSONObject instructions_tr_th_td(Element element)
	{	
		JSONObject json = new JSONObject(true);
		Elements trs = element.getElementsByTag("tr");
		for (Element e : trs) {
			Element th = e.getElementsByTag("th").first();
			Element td = e.getElementsByTag("td").first();
			
			if(th!=null&&td!=null)
			{
				String key =StringUtil.cleanKey(th.text());
				String value=StringUtil.RemoveEmpty(td.text());
				json.put(key, value);	
			}
		}
		
		return name_number(json);	
	}
	protected JSONObject instructions_tr_th_td(Element element,JSONObject json)
	{	
		Elements trs = element.getElementsByTag("tr");
		for (Element e : trs) {
			Element th = e.getElementsByTag("th").first();
			Element td = e.getElementsByTag("td").first();
			
			if(th!=null&&td!=null)
			{
				String key =StringUtil.cleanKey(th.text());
				String value=StringUtil.RemoveEmpty(td.text());
				json.put(key, value);	
			}
		}
		
		return name_number(json);	
	}
	protected JSONObject instructions_tr_td_td(Element element) {
		
		JSONObject json = new JSONObject(true);
		Elements trs = element.getElementsByTag("tr");
		for (Element tr : trs) {
			Elements tds = tr.getElementsByTag("td");
			if(tds.size()==2)
			{
				String key=StringUtil.cleanKey(tds.get(0).text());
				String value=StringUtil.RemoveEmpty(tds.get(1).text());
				if("批准文号".equals(key))
				{
						json.put(key, value.replaceAll("查询该产品备案信息", ""));
				}else
				{
					json.put(key, value);	
				}
			
			}
		}
		return name_number(json);
	}
	protected JSONObject instructions_tr_td_td(Element element,JSONObject json) {
		
		Elements trs = element.getElementsByTag("tr");
		for (Element tr : trs) {
			Elements tds = tr.getElementsByTag("td");
			if(tds.size()==2)
			{
				String key=StringUtil.cleanKey(tds.get(0).text());
				String value=StringUtil.RemoveEmpty(tds.get(1).text());
				json.put(key, value);
			}
		}
		return name_number(json);
	}
	protected JSONObject instructions_s(String text) {
		JSONObject json = new JSONObject(true);
		String[] ps = StringUtil.split(text, "【");
		for (String p : ps) {
			String[] es = StringUtil.split(p, "】");
			if(es.length==2) {
				String key=StringUtil.cleanKey(es[0]);
				String value=StringUtil.RemoveEmpty(es[1]);
				json.put(key, value);	
				
			}
		}
		
		return name_number(json);
	}
	protected JSONObject instructions_s(String text,JSONObject json) {
		String[] ps = StringUtil.split(text, "【");
		for (String p : ps) {
			String[] es = StringUtil.split(p, "】");
			if(es.length==2) {
				String key=StringUtil.cleanKey(es[0]);
				String value=StringUtil.RemoveEmpty(es[1]);
				json.put(key, value);	
				
			}
		}
		
		return name_number(json);
	}
	protected JSONObject instructions_dl_dt_dd(Element element) {
		JSONObject json = new JSONObject(true);
	
		Elements dls = element.getElementsByTag("dl");
		for (Element dl : dls) {
			Element dt = dl.getElementsByTag("dt").first();
			Element dd = dl.getElementsByTag("dd").first();
			if(dt!=null&&dd!=null) {
				String key=StringUtil.cleanKey(dt.text());
				String value=StringUtil.RemoveEmpty(dd.text());
				json.put(key, value);
			}
		}		
		return name_number(json);
	}
	protected JSONObject instructions_dl_dt_dd(Element element,JSONObject json ) {
	
		Elements dls = element.getElementsByTag("dl");
		for (Element dl : dls) {
			Element dt = dl.getElementsByTag("dt").first();
			Element dd = dl.getElementsByTag("dd").first();
			if(dt!=null&&dd!=null) {
				String key=StringUtil.cleanKey(dt.text());
				String value=StringUtil.RemoveEmpty(dd.text());
				json.put(key, value);
			}
		}		
		return name_number(json);
	}
	protected JSONObject name_number(JSONObject instructions) {
		
		String name_value = instructions.getString("药品名称");
		if(StringUtil.isEmpty(name_value))name_value = instructions.getString("药物名称");
		if(StringUtil.isEmpty(name_value))name_value = instructions.getString("产品名称");	
		if(name_value!=null)
		{  
			JSONObject jsonObject = new JSONObject(true);
			if(name_value.indexOf("：")>=0||name_value.indexOf(":")>=0)
			{
				String s = replaceAll(name_value);
				
				String[] ts = StringUtil.split(s, "|");
				for (String e : ts) {
					String[] pes = StringUtil.split(e, "：");		
					if(pes.length==2)	
						jsonObject .put(StringUtil.cleanKey(pes[0]),StringUtil.RemoveEmpty(pes[1]));
					pes = StringUtil.split(e, ":");		
					if(pes.length==2)	
						jsonObject.put(StringUtil.cleanKey(pes[0]),StringUtil.RemoveEmpty(pes[1]));	
				}
			}else
			{
				
				jsonObject.put("通用名称", name_value);
			}
			Set<String> keys = instructions.keySet();
			for (String e : keys) {		
				jsonObject.put(e, instructions.get(e));
			};
			instructions=jsonObject;
			instructions.remove("药品名称");
			instructions.remove("药物名称");
		}
		if(instructions!=null)
		{
			String name = instructions.getString("通用名称");
			if(StringUtil.isEmpty(name))name= instructions.getString("通用名");
			if(StringUtil.isEmpty(name))name= instructions.getString("商品名称");
			if(StringUtil.isEmpty(name))name= instructions.getString("药品名称");
			json.put("name", name);
			json.put("number", instructions.getString("批准文号"));
			String number = json.getString("number");
			if(StringUtil.isNotEmpty(number))
			{
				 char[] ch = number.toCharArray();
				 for(int i = 0; i < ch.length ; i++){
		             if(ch[i] >= 'A' && ch[i] <= 'Z'){
		            	 json.put("gtype", ch[i]);
		             }
		         }
			}
		}
		instructions.remove("商品编号");
		instructions.remove("温馨提示");
		instructions.remove("商品编码");
		instructions.remove("警示语");
		instructions.remove("品牌");
		String ptype = instructions.getString("批准文号");
		if(StringUtil.isNotEmpty(ptype))
		{
			instructions.put("批准文号", StringUtil.remove(ptype, "(国家食药局查询)"));
		}
		
		return instructions;
		
		
	}
	
	protected String replaceAll(String str)
	{
		return str.replaceAll("商品编码", "|商品编码").replaceAll("批准文号", "|批准文号").replaceAll("生产厂家", "|生产厂家").replaceAll("通用名", "|通用名").replaceAll("汉语拼音", "|汉语拼音").replaceAll("拼音全码", "|拼音全码").replaceAll("商品名称", "|商品名称").replaceAll("英文名", "|英文名").replaceAll("汉语拼音", "|汉语拼音").replaceAll("商品名", "|商品名");
	}
}
